//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.ww.springboot.dd.amazon;

import java.net.URI;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;

import com.ww.springboot.dd.api.R;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

public class HttpUtil {
    private static final Logger log = LoggerFactory.getLogger(HttpUtil.class);
    private static RestTemplate httpRestTemplateReport;
    private static RestTemplate httpRestTemplate;
    private static final int REPORT_MAX_TIMEOUT = 1800000;
    private static final int MAX_TIMEOUT = 60000;

    public HttpUtil() {
    }

    public static <T> T get(String url, Class<T> clazz) {
        HttpParams<String> httpParams = new HttpParams();
        httpParams.setUrl(url);
        httpParams.setHttpMethod(HttpMethod.GET);
        httpParams.addHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36");
        return exchange(httpParams, clazz);
    }

    public static <T> T get(URI url, Class<T> clazz) {
        HttpParams<String> httpParams = new HttpParams();
        httpParams.setUri(url);
        httpParams.setHttpMethod(HttpMethod.GET);
        return exchange(httpParams, clazz);
    }

    public static <R, T> R post(String url, T body, Class<R> clazz) {
        HttpParams<T> httpParams = new HttpParams();
        httpParams.setUrl(url);
        httpParams.setHttpMethod(HttpMethod.POST);
        httpParams.setBody(body);
        return exchange(httpParams, clazz);
    }

    public static <R, T> R put(String url, T body, Class<R> clazz) {
        HttpParams<T> httpParams = new HttpParams();
        httpParams.setUrl(url);
        httpParams.setHttpMethod(HttpMethod.PUT);
        httpParams.setBody(body);
        return exchange(httpParams, clazz);
    }

    public static R<String> get(String url) {
        HttpParams<String> httpParams = new HttpParams();
        httpParams.setUrl(url);
        httpParams.setHttpMethod(HttpMethod.GET);

        try {
            return (R)JSON.parseObject((String)exchange(httpParams, String.class), new TypeReference<R<String>>() {
            }, new Feature[0]);
        } catch (Exception var3) {
            Exception e = var3;
            log.error(e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    public static <T> R<String> post(String url, T body) {
        HttpParams<T> httpParams = new HttpParams();
        httpParams.setUrl(url);
        httpParams.setHttpMethod(HttpMethod.POST);
        httpParams.setBody(body);

        try {
            return (R)JSON.parseObject((String)exchange(httpParams, String.class), new TypeReference<R<String>>() {
            }, new Feature[0]);
        } catch (Exception var4) {
            Exception e = var4;
            log.error(e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    public static <T> R<String> delete(String url, T body) {
        HttpParams<T> httpParams = new HttpParams();
        httpParams.setUrl(url);
        httpParams.setHttpMethod(HttpMethod.DELETE);
        httpParams.setBody(body);

        try {
            return (R)JSON.parseObject((String)exchange(httpParams, String.class), new TypeReference<R<String>>() {
            }, new Feature[0]);
        } catch (Exception var4) {
            Exception e = var4;
            log.error(e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

    public static <T, R> R exchange(HttpParams<T> httpParams, Class<R> resultType) {
        if (checkHttpParams(httpParams) && resultType != null) {
            HttpEntity<T> httpEntity = new HttpEntity(httpParams.getBody(), httpParams.getHttpHeaders());
            ResponseEntity response;
            if (httpParams.getUri() == null) {
                response = httpRestTemplate.exchange(httpParams.getUrl(), httpParams.getHttpMethod(), httpEntity, resultType, new Object[0]);
                return (R) response.getBody();
            } else {
                response = httpRestTemplate.exchange(httpParams.getUri(), httpParams.getHttpMethod(), httpEntity, resultType);
                return (R) response.getBody();
            }
        } else {
            log.warn("http params  can not be satisfy to exchange http.");
            return null;
        }
    }

    public static <T> T getReport(String url, Class<T> clazz) {
        HttpParams<String> httpParams = new HttpParams();
        httpParams.setUrl(url);
        httpParams.setHttpMethod(HttpMethod.GET);
        return exchangeReport(httpParams, clazz);
    }

    public static <R, T> R postReport(String url, T body, Class<R> clazz) {
        HttpParams<T> httpParams = new HttpParams();
        httpParams.setUrl(url);
        httpParams.setHttpMethod(HttpMethod.POST);
        httpParams.setBody(body);
        return exchangeReport(httpParams, clazz);
    }

    public static <T, R> R exchangeReport(HttpParams<T> httpParams, Class<R> resultType) {
        if (checkHttpParams(httpParams) && resultType != null) {
            HttpEntity<T> httpEntity = new HttpEntity(httpParams.getBody(), httpParams.getHttpHeaders());
            ResponseEntity<R> response = httpRestTemplateReport.exchange(httpParams.getUrl(), httpParams.getHttpMethod(), httpEntity, resultType, new Object[0]);
            return response.getBody();
        } else {
            log.warn("http params  can not be satisfy to exchange http.");
            return null;
        }
    }

    private static boolean checkHttpParams(HttpParams<?> httpParams) {
        return httpParams != null && (!StringUtils.isEmpty(httpParams.getUrl()) || httpParams.getUri() != null);
    }

    static {
        SimpleClientHttpRequestFactory factoryReport = new SimpleClientHttpRequestFactory();
        factoryReport.setConnectTimeout(1800000);
        factoryReport.setReadTimeout(1800000);
        httpRestTemplateReport = new RestTemplate(factoryReport);
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setConnectTimeout(60000);
        factory.setReadTimeout(60000);
        httpRestTemplate = new RestTemplate(factory);
    }
}
